/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package web;


import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.ws.WebServiceRef;

/**
 *
 * @author Max
 */
public class PrenotazioneHotel extends HttpServlet {
    @WebServiceRef(wsdlLocation = "WEB-INF/wsdl/localhost_8080/HotelWebService/HotelWebService.wsdl")
    private HotelWebService_Service service;
    

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession s = request.getSession();
        System.out.println("Passo dalla servlet PrenotazioneHotel Banane");
        //int numLetti = Integer.parseInt(request.getParameter("tipoStanza"));
        //System.out.println("Stanza" + numLetti);
        int quantitaStanzeSingole = Integer.parseInt(request.getParameter("numeroStanzeSingole"));
        System.out.println("quantita stanze "+ quantitaStanzeSingole);
        int quantitaStanzeMatrimoniali = Integer.parseInt(request.getParameter("numeroStanzeMatrimoniali"));
        System.out.println("quantita stanze "+ quantitaStanzeMatrimoniali);
        String nome = request.getParameter("nome");
        String cognome = request.getParameter("cognome");
        String username = (String)s.getAttribute("username");
        int done = 1;
        for(int i = 1;i<=quantitaStanzeSingole;i++)
            done = done * prenotaCamera(1, nome, cognome, username);
        for(int i = 1;i<=quantitaStanzeMatrimoniali;i++)
            done = done * prenotaCamera(2, nome, cognome, username);
        request.setAttribute("statoPrenotazione", done);
        RequestDispatcher rd = getServletContext().getRequestDispatcher("/hotel.jsp");
        rd.forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private int prenotaCamera(int numLetti, java.lang.String nome, java.lang.String cognome, java.lang.String username) {
        web.HotelWebService port = service.getHotelWebServicePort();
        return port.prenotaCamera(numLetti, nome, cognome, username);
    }

    
}
